/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OptionBix.java
 *
 * Created on 15 févr. 2012, 16:03:12
 */
package gov.nist.ssd.swqb.gui.boxes;

import gov.nist.ssd.swqb.config.DebugMode;
import gov.nist.ssd.swqb.config.RequestsTimeout;
import gov.nist.ssd.swqb.core.files.OptionManager;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

/**
 *
 * @author Benjamin Herbomez
 */
public class OptionBox extends javax.swing.JDialog {

    private final OptionManager optionManager;

    public OptionBox(JFrame parent, boolean modal, OptionManager optionManager) {
        super(parent, modal);
        this.optionManager = optionManager;
        initComponents();
        this.init();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel6 = new javax.swing.JPanel();
        jSeparator2 = new javax.swing.JSeparator();
        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel3 = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        defaultFolderField = new javax.swing.JTextField();
        defaultFolderButton = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        timeoutSpinner = new javax.swing.JSpinner();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        defaultDisplayCountSpinner = new javax.swing.JSpinner();
        animationDelaySpinner = new javax.swing.JSpinner();
        jPanel4 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        debugModeToggleButton = new javax.swing.JToggleButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Options"); // NOI18N

        jPanel6.setName("jPanel6"); // NOI18N
        jPanel6.setLayout(new java.awt.BorderLayout());

        jSeparator2.setName("jSeparator2"); // NOI18N
        jPanel6.add(jSeparator2, java.awt.BorderLayout.SOUTH);

        jPanel7.setName("jPanel7"); // NOI18N

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 15));
        jLabel2.setText("Options");
        jLabel2.setName("jLabel2"); // NOI18N
        jPanel7.add(jLabel2);

        jPanel6.add(jPanel7, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel6, java.awt.BorderLayout.NORTH);

        jPanel1.setName("jPanel1"); // NOI18N
        jPanel1.setLayout(new java.awt.BorderLayout());

        jSeparator1.setName("jSeparator1"); // NOI18N
        jPanel1.add(jSeparator1, java.awt.BorderLayout.NORTH);

        jPanel3.setName("jPanel3"); // NOI18N
        jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        saveButton.setText("Save");
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });
        jPanel3.add(saveButton);

        applyButton.setText("Apply");
        applyButton.setName("applyButton"); // NOI18N
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });
        jPanel3.add(applyButton);

        closeButton.setText("Close");
        closeButton.setName("closeButton"); // NOI18N
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });
        jPanel3.add(closeButton);

        jPanel1.add(jPanel3, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setLayout(new java.awt.GridBagLayout());

        jPanel5.setName("jPanel5"); // NOI18N
        jPanel5.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Default folder");
        jLabel1.setName("jLabel1"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel5.add(jLabel1, gridBagConstraints);

        defaultFolderField.setMinimumSize(new java.awt.Dimension(250, 20));
        defaultFolderField.setName("defaultFolderField"); // NOI18N
        defaultFolderField.setPreferredSize(new java.awt.Dimension(250, 20));
        defaultFolderField.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                defaultFolderFieldMousePressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
        jPanel5.add(defaultFolderField, gridBagConstraints);

        defaultFolderButton.setText("open");
        defaultFolderButton.setName("defaultFolderButton"); // NOI18N
        defaultFolderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                defaultFolderButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        jPanel5.add(defaultFolderButton, gridBagConstraints);

        jLabel4.setText("Requests timeout");
        jLabel4.setName("jLabel4"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel5.add(jLabel4, gridBagConstraints);

        jLabel5.setText(" : ");
        jLabel5.setName("jLabel5"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        jPanel5.add(jLabel5, gridBagConstraints);

        jLabel6.setText(" : ");
        jLabel6.setName("jLabel6"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        jPanel5.add(jLabel6, gridBagConstraints);

        jLabel7.setText("(millisecond)");
        jLabel7.setName("jLabel7"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        jPanel5.add(jLabel7, gridBagConstraints);

        timeoutSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(0), null, Integer.valueOf(1)));
        timeoutSpinner.setName("timeoutSpinner"); // NOI18N
        timeoutSpinner.setPreferredSize(new java.awt.Dimension(60, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel5.add(timeoutSpinner, gridBagConstraints);

        jLabel10.setName("jLabel10"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        jPanel5.add(jLabel10, gridBagConstraints);

        jLabel11.setText("(to disable timeout set to 0)");
        jLabel11.setName("jLabel11"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        jPanel5.add(jLabel11, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 15;
        gridBagConstraints.ipady = 15;
        jPanel2.add(jPanel5, gridBagConstraints);

        jTabbedPane1.addTab("General", jPanel2);

        jPanel8.setName("jPanel8"); // NOI18N
        jPanel8.setLayout(new java.awt.GridBagLayout());

        jPanel9.setName("jPanel9"); // NOI18N
        jPanel9.setLayout(new java.awt.GridBagLayout());

        jLabel3.setText("Default display count");
        jLabel3.setName("jLabel3"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel9.add(jLabel3, gridBagConstraints);

        jLabel14.setText(" : ");
        jLabel14.setName("jLabel14"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        jPanel9.add(jLabel14, gridBagConstraints);

        jLabel12.setText("Animation delay (second)");
        jLabel12.setName("jLabel12"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel9.add(jLabel12, gridBagConstraints);

        jLabel13.setText(" : ");
        jLabel13.setName("jLabel13"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel9.add(jLabel13, gridBagConstraints);

        defaultDisplayCountSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(1), Integer.valueOf(1), null, Integer.valueOf(1)));
        defaultDisplayCountSpinner.setName("defaultDisplayCountSpinner"); // NOI18N
        defaultDisplayCountSpinner.setPreferredSize(new java.awt.Dimension(40, 20));
        jPanel9.add(defaultDisplayCountSpinner, new java.awt.GridBagConstraints());

        animationDelaySpinner.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(50.0f), Float.valueOf(0.0f), null, Float.valueOf(0.1f)));
        animationDelaySpinner.setName("animationDelaySpinner"); // NOI18N
        animationDelaySpinner.setPreferredSize(new java.awt.Dimension(40, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel9.add(animationDelaySpinner, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 15;
        gridBagConstraints.ipady = 15;
        jPanel8.add(jPanel9, gridBagConstraints);

        jTabbedPane1.addTab("Gaphic", jPanel8);

        jPanel4.setName("jPanel4"); // NOI18N
        jPanel4.setLayout(new java.awt.GridBagLayout());

        jPanel10.setName("jPanel10"); // NOI18N
        jPanel10.setLayout(new java.awt.GridBagLayout());

        jLabel8.setText("Debug mode");
        jLabel8.setName("jLabel8"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        jPanel10.add(jLabel8, gridBagConstraints);

        jLabel9.setText(" : ");
        jLabel9.setName("jLabel9"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        jPanel10.add(jLabel9, gridBagConstraints);

        debugModeToggleButton.setText("On");
        debugModeToggleButton.setName("debugModeToggleButton"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        jPanel10.add(debugModeToggleButton, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.ipadx = 15;
        gridBagConstraints.ipady = 15;
        jPanel4.add(jPanel10, gridBagConstraints);

        jTabbedPane1.addTab("Dev", jPanel4);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
            this.dispose();
	}//GEN-LAST:event_closeButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        this.save();
        this.dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void defaultFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_defaultFolderButtonActionPerformed

        this.selectDefaultFolder();
	}//GEN-LAST:event_defaultFolderButtonActionPerformed

    private void defaultFolderFieldMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_defaultFolderFieldMousePressed

        this.selectDefaultFolder();
	}//GEN-LAST:event_defaultFolderFieldMousePressed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        this.save();
    }//GEN-LAST:event_applyButtonActionPerformed

    private void selectDefaultFolder() {
        int returnVal = fc.showDialog(this, "Choose default folder");
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            this.defaultFolderField.setText(fc.getSelectedFile().getAbsolutePath());
        }
    }

    private void init() {
        this.fc = new JFileChooser();
        this.fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        this.initValues();
    }
    
    private void initValues(){
        //Set default foler if exists
        String defFolder = this.optionManager.getOption("default_folder");
        if (defFolder != null) {
            fc.setCurrentDirectory(new File(defFolder));
            this.defaultFolderField.setText(defFolder);
        }
        Integer defDisplayCount = this.optionManager.getOptionAsInt("default_display_count");
        if(defDisplayCount != null){
            this.defaultDisplayCountSpinner.setValue(defDisplayCount);
        }
        
        //Debug button
        this.debugModeToggleButton.setSelected(DebugMode.debugModeIsOn());
        
        //Timeout
        this.timeoutSpinner.setValue(RequestsTimeout.requestsTimeout());
     
        //Animation
        Float delay = this.optionManager.getOptionAsFloat("animation_Delay");
        if(delay != null)
            this.animationDelaySpinner.setValue(delay);
        else{
            this.animationDelaySpinner.setValue((float)0.5);
            this.optionManager.setOption("animation_Delay","0.5");
        }
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
        if(b)
            this.initValues();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSpinner animationDelaySpinner;
    private javax.swing.JButton applyButton;
    private javax.swing.JButton closeButton;
    private javax.swing.JToggleButton debugModeToggleButton;
    private javax.swing.JSpinner defaultDisplayCountSpinner;
    private javax.swing.JButton defaultFolderButton;
    private javax.swing.JTextField defaultFolderField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton saveButton;
    private javax.swing.JSpinner timeoutSpinner;
    // End of variables declaration//GEN-END:variables
    private JFileChooser fc;

    private void save() {
        this.optionManager.setOption("default_folder", this.defaultFolderField.getText());
        this.optionManager.setOption("default_display_count", this.defaultDisplayCountSpinner.getValue().toString().trim());
        this.optionManager.setOption("animation_Delay", this.animationDelaySpinner.getValue().toString().trim());
        this.optionManager.setOption("default_timemout", this.timeoutSpinner.getValue().toString().trim());
            RequestsTimeout.setRequestTimeout(new Integer(this.timeoutSpinner.getValue().toString()));
        String debug = "0";
        if(this.debugModeToggleButton.isSelected())
            debug = "1";
        this.optionManager.setOption("debug_mode", debug);
            DebugMode.setDebugMode(this.debugModeToggleButton.isSelected());
    }
}
